﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace kim.DAL
{
    /// <summary>
    /// 数据访问类:ListTypea
    /// </summary>
    public partial class ListTypea
    {
        public ListTypea()
        { }
        #region  Method
        /// <summary>
        /// 得到最大ID
        /// </summary>
      
        public DataSet GetNewsPager(string tbname, string FieldKey, int PageCurrent,
int PageSize, string FieldShow, string FieldOrder, string Where)
        {
            SqlParameter[] parameters ={
               new SqlParameter("@tbname",SqlDbType.VarChar,128),
               new SqlParameter("@FieldKey",SqlDbType.VarChar,1000),
               new SqlParameter("@PageCurrent",SqlDbType.Int),
               new SqlParameter("@PageSize",SqlDbType.Int),
               new SqlParameter("@FieldShow",SqlDbType.NVarChar,1000),
               new SqlParameter("@FieldOrder",SqlDbType.NVarChar,1000),
               new SqlParameter("@Where",SqlDbType.NVarChar,1000),
               new SqlParameter("@ItemCount",SqlDbType.Int),
                new SqlParameter("@PageCount",SqlDbType.Int)};

            parameters[0].Value = tbname;
            parameters[1].Value = FieldKey;
            parameters[2].Value = PageCurrent;
            parameters[3].Value = PageSize;
            parameters[4].Value = FieldShow;
            parameters[5].Value = FieldOrder;
            parameters[6].Value = Where;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            return Common.SqlDbHelper.GetDataSetProc("sp_PageView", parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(kim.Model.ListTypea model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ListType(");
            strSql.Append("NeType,NeName,NeTime,ListType,ListorOne,ListPosition,IsParent,ListUrl,NeEnName)");
            strSql.Append(" values (");
            strSql.Append("@NeType,@NeName,@NeTime,@ListType,@ListorOne,@ListPosition,@IsParent,@ListUrl,@NeEnName)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@NeType", SqlDbType.Int,4),
					new SqlParameter("@NeName", SqlDbType.NVarChar,50),
					new SqlParameter("@NeTime", SqlDbType.DateTime),
					new SqlParameter("@ListType", SqlDbType.Int,4),
					new SqlParameter("@ListorOne", SqlDbType.Int,4),
					new SqlParameter("@ListPosition", SqlDbType.Int,4),
					new SqlParameter("@IsParent", SqlDbType.Int,4),
					new SqlParameter("@ListUrl", SqlDbType.NVarChar,100),
					new SqlParameter("@NeEnName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.NeType;
            parameters[1].Value = model.NeName;
            parameters[2].Value = model.NeTime;
            parameters[3].Value = model.ListType;
            parameters[4].Value = model.ListorOne;
            parameters[5].Value = model.ListPosition;
            parameters[6].Value = model.IsParent;
            parameters[7].Value = model.ListUrl;
            parameters[8].Value = model.NeEnName;

            object obj =  kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(kim.Model.ListTypea model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ListType set ");
            strSql.Append("NeType=@NeType,");
            strSql.Append("NeName=@NeName,");
            strSql.Append("NeTime=@NeTime,");
            strSql.Append("ListType=@ListType,");
            strSql.Append("ListorOne=@ListorOne,");
            strSql.Append("ListPosition=@ListPosition,");
            strSql.Append("IsParent=@IsParent,");
            strSql.Append("ListUrl=@ListUrl,");
            strSql.Append("NeEnName=@NeEnName");
            strSql.Append(" where ListID=@ListID");
            SqlParameter[] parameters = {
					new SqlParameter("@NeType", SqlDbType.Int,4),
					new SqlParameter("@NeName", SqlDbType.NVarChar,50),
					new SqlParameter("@NeTime", SqlDbType.DateTime),
					new SqlParameter("@ListType", SqlDbType.Int,4),
					new SqlParameter("@ListorOne", SqlDbType.Int,4),
					new SqlParameter("@ListPosition", SqlDbType.Int,4),
					new SqlParameter("@IsParent", SqlDbType.Int,4),
					new SqlParameter("@ListUrl", SqlDbType.NVarChar,100),
					new SqlParameter("@NeEnName", SqlDbType.NVarChar,50),
					new SqlParameter("@ListID", SqlDbType.Int,4)};
            parameters[0].Value = model.NeType;
            parameters[1].Value = model.NeName;
            parameters[2].Value = model.NeTime;
            parameters[3].Value = model.ListType;
            parameters[4].Value = model.ListorOne;
            parameters[5].Value = model.ListPosition;
            parameters[6].Value = model.IsParent;
            parameters[7].Value = model.ListUrl;
            parameters[8].Value = model.NeEnName;
            parameters[9].Value = model.ListID;

            int rows =  kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ListID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ListType ");
            strSql.Append(" where ListID=@ListID");
            SqlParameter[] parameters = {
					new SqlParameter("@ListID", SqlDbType.Int,4)
			};
            parameters[0].Value = ListID;

            int rows =  kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ListIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ListTypea ");
            strSql.Append(" where ListID in (" + ListIDlist + ")  ");
            int rows =  kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public kim.Model.ListTypea GetModel(int ListID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ListID,NeType,NeName,NeTime,ListType,ListorOne,ListPosition,IsParent,ListUrl,NeEnName from ListType ");
            strSql.Append(" where ListID=@ListID");
            SqlParameter[] parameters = {
					new SqlParameter("@ListID", SqlDbType.Int,4)
			};
            parameters[0].Value = ListID;

            kim.Model.ListTypea model = new kim.Model.ListTypea();
            DataSet ds =  kim.DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ListID"] != null && ds.Tables[0].Rows[0]["ListID"].ToString() != "")
                {
                    model.ListID = int.Parse(ds.Tables[0].Rows[0]["ListID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NeType"] != null && ds.Tables[0].Rows[0]["NeType"].ToString() != "")
                {
                    model.NeType = int.Parse(ds.Tables[0].Rows[0]["NeType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NeName"] != null && ds.Tables[0].Rows[0]["NeName"].ToString() != "")
                {
                    model.NeName = ds.Tables[0].Rows[0]["NeName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NeTime"] != null && ds.Tables[0].Rows[0]["NeTime"].ToString() != "")
                {
                    model.NeTime = DateTime.Parse(ds.Tables[0].Rows[0]["NeTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ListType"] != null && ds.Tables[0].Rows[0]["ListType"].ToString() != "")
                {
                    model.ListType = int.Parse(ds.Tables[0].Rows[0]["ListType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ListorOne"] != null && ds.Tables[0].Rows[0]["ListorOne"].ToString() != "")
                {
                    model.ListorOne = int.Parse(ds.Tables[0].Rows[0]["ListorOne"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ListPosition"] != null && ds.Tables[0].Rows[0]["ListPosition"].ToString() != "")
                {
                    model.ListPosition = int.Parse(ds.Tables[0].Rows[0]["ListPosition"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsParent"] != null && ds.Tables[0].Rows[0]["IsParent"].ToString() != "")
                {
                    model.IsParent = int.Parse(ds.Tables[0].Rows[0]["IsParent"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ListUrl"] != null && ds.Tables[0].Rows[0]["ListUrl"].ToString() != "")
                {
                    model.ListUrl = ds.Tables[0].Rows[0]["ListUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NeEnName"] != null && ds.Tables[0].Rows[0]["NeEnName"].ToString() != "")
                {
                    model.NeEnName = ds.Tables[0].Rows[0]["NeEnName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ListID,NeType,NeName,NeTime,ListType,ListorOne,ListPosition,IsParent,ListUrl,NeEnName ");
            strSql.Append(" FROM ListType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return  kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ListID,NeType,NeName,NeTime,ListType,ListorOne,ListPosition,IsParent,ListUrl,NeEnName ");
            strSql.Append(" FROM ListType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return  kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM ListType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj =  kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ListID desc");
            }
            strSql.Append(")AS Row, T.*  from ListTypea T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return  kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ListTypea";
            parameters[1].Value = "ListID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return  kim.DBUtility.DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

